package cn.chengpengper.seckill.common.exception;

import cn.chengpengper.seckill.common.code.HttpCode;
import cn.chengpengper.seckill.common.response.ResponseMessage;
import cn.chengpengper.seckill.common.response.ResponseMessageBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author chengpeng.hu
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(SeckillException.class)
    public ResponseMessage<String> handleSeckillException(SeckillException e) {
        log.error("服务器异常:", e);
        return ResponseMessageBuilder.build(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public ResponseMessage<String> handleException(Exception e) {
        log.error("服务器异常：", e);
        return ResponseMessageBuilder.build(HttpCode.SERVER_EXCEPTION.getCode(), e.getMessage());
    }

}
